package com.globalwave.system.service;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.exception.BusinessException;
import com.globalwave.system.entity.OrganizationPrivilege;
import com.globalwave.system.entity.Privilege;
import com.globalwave.system.entity.PrivilegeCriterion;

public class PrivilegeServiceImpl 
       extends BaseServiceImpl 
       implements PrivilegeService {

    public Privilege create(Privilege privilege) throws Exception {
    	
    	Long pid = privilege.getPro_privilege_id() ;
    	
    	if (pid != null && lock(pid) == 0) {
    		throw new BusinessException("1031") ;//1031', '父权限不存在，本操作无效！
    	}
    	
        return (Privilege) jdbcDao.insert(privilege);
    }

    public void update(Privilege privilege) throws Exception {
    	
    	lock(privilege.getPro_privilege_id()) ;
    	
        jdbcDao.update(privilege) ;
    }

    public void delete(Privilege privilege) throws Exception {
    	
    	lock(privilege.getPro_privilege_id()) ;
    	
    	if (hasChildren(privilege.getPrivilege_id())) {
    		throw new BusinessException("1032") ;//1032', '子权限存在，本操作无效！
    	}
    	
        // delete cascade 
        OrganizationPrivilege orgPrivilege = new OrganizationPrivilege() ;
        orgPrivilege.setPrivilege_id(privilege.getPrivilege_id()) ;
        jdbcDao.delete(OrganizationPrivilege.class, orgPrivilege) ;
        
        jdbcDao.delete(privilege) ;
        
    }

    public void deleteAll(Long[] privilegeIds) throws Exception {
    	//lockPrivilege(jdbcDao.get(object)) ;--lock!!
    	
    	for (Long privilegeId:privilegeIds) {
        	if (hasChildren(privilegeId)) {
        		throw new BusinessException("1032") ;//1032', '子权限存在，本操作无效！
        	}
    	}
    	
        PrivilegeCriterion criterion = new PrivilegeCriterion() ;
        criterion.setPrivilegeIds(privilegeIds) ;
        
        jdbcDao.delete(OrganizationPrivilege.class, criterion) ;
        jdbcDao.delete(Privilege.class, criterion) ;
        
    }
    /*
    public ArrayPageList<Privilege> getChildren(Long privilegeId) throws Exception {
    	Privilege so = new Privilege() ;
    	so.setPro_privilege_id(privilegeId) ;
    	
    	return jdbcDao.query(Privilege.class, so, null) ;
    }*/

    private boolean hasChildren(Long privilegeId) throws Exception {
    	Privilege so = new Privilege() ;
    	so.setPro_privilege_id(privilegeId) ;
    	return jdbcDao.getName("systemSQLs:privilegeChildrenCount", so, 0L) > 0 ;
    }
    
    private int lock(Long privilegeId) throws Exception {
    	if (privilegeId == null) {
    		return 0;
    	}
    	Privilege p = new Privilege() ;
    	p.setPrivilege_id(privilegeId) ;
    	p.addInclusions("privilege_id") ;
    	
    	return jdbcDao.update(p) ;
    }

    public ArrayPageList<Privilege> query(
            PrivilegeCriterion privilegeCriterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            orderList.addAsc("privilege_id") ;
        }
        
        return (ArrayPageList<Privilege>)jdbcDao.query(privilegeCriterion, pageIndex, pageSize, orderList, Privilege.class);
    }


    public ArrayPageList<Privilege> possessedByUser(Long userId) throws Exception {
        String Sql = String.format(PrivilegeCriterion.PRIVILEGE_PROSESSED_BY_USER, userId) ;
        return (ArrayPageList<Privilege>)jdbcDao.query(new StringBuffer(Sql), null, null, Privilege.class);
    }
    
    public ArrayPageList<Privilege> possessedByOrganization(Long organizationId) throws Exception {
        String Sql = String.format(PrivilegeCriterion.PRIVILEGE_PROSESSED_BY_ORGANIZATION, organizationId) ;
        return (ArrayPageList<Privilege>)jdbcDao.query(new StringBuffer(Sql), null, null, Privilege.class);
    }

    public ArrayPageList<Privilege> getAllByUserId(Long user_id) throws Exception {
        String Sql = String.format(PrivilegeCriterion.USER_ALL_PRIVILEGE, user_id) ;
        
        ArrayPageList<Privilege> privileges =
            (ArrayPageList<Privilege>)jdbcDao.query(new StringBuffer(Sql), null, null, Privilege.class);      
        

        
        return privileges ;
    }
}
